// pages/experience/operatorInfo/operatorInfo.js
import Http from '../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        operatorArr: [],
        coverTitle: '', //弹窗标题
        coverContent: '', //弹窗ussd内容
        remarkContent: '', //弹窗备注内容
        detailShow: false, //备注弹窗
        phoneDetail: false, //ussd弹窗
        pageNum: 0,
        apnMaxCount: '', //apn数量
        countryCount: '', //国家数
        operatorNum: Number, //运营商数量
        viewNum: Number, //当前视图数量
        loadingList: true, //是否加载全部
    },
    //查看国家、运营商列表
    jumpCityListTap(e) {
        wx.navigateTo({
            url: `../../../functionPage/pages/operatorInfo/countryList/countryList`,
        });
    },
    //再次编辑
    jumpEditTap(e) {
        if (app.powerIdentity('00,01,02,03,04,05,06')) return;
        wx.navigateTo({
            url: `../../../releasePage/pages/addOperator/addOperator?id=${e.target.dataset.id}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getOperatorList();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    refresh() {
        let _that = this;
        _that.setData({
            operatorArr: [],
            detailShow: false, //备注弹窗
            phoneDetail: false, //ussd弹窗
            loadingList: true,
            pageNum: 0,
        });
        if (_that.data.id) {
            _that.getOperatorListById();
        } else {
            _that.getOperatorList();
        }
    },
    // 对比加载
    balanceInfo() {
        this.setData({
            operatorArr: [],
            detailShow: false, //备注弹窗
            phoneDetail: false, //ussd弹窗
            apnMaxCount: '', //apn数量
            countryCount: '', //国家数
        });
        this.getOperatorListById();
    },
    preventD(e) {
        return;
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    getOperatorList() {
        let _that = this;
        if (_that.data.loadingList && !_that.data.id) {
            _that.setData({
                pageNum: _that.data.pageNum + 1,
            });
            wx.showLoading({
                title: '加载中...',
            });
            let data = {
                authId: app.globalData.authId,
                pageSize: 15,
                pageNo: _that.data.pageNum,
            };
            http.getRequest(`cjMap/getCityOperatorList`, data).then((res) => {
                setTimeout(function () {
                    wx.hideLoading();
                }, 1000);
                if (res.status == 0) {
                    for (let i = 0; i < res.data.length; i++) {
                        let tempNetworkSupportArr = [
                            {
                                name: 'NR',
                                value: '00',
                                checked: false,
                            },
                            {
                                name: 'ViWiFi',
                                value: '01',
                                checked: false,
                            },
                            {
                                name: 'ViLTE',
                                value: '03',
                                checked: false,
                            },
                            {
                                name: 'VoWiFi',
                                value: '02',
                                checked: false,
                            },
                            {
                                name: 'VoLTE',
                                value: '06',
                                checked: false,
                            },
                            {
                                name: 'LTE',
                                value: '04',
                                checked: false,
                            },
                            {
                                name: '3G',
                                value: '07',
                                checked: false,
                            },
                            {
                                name: '2G',
                                value: '08',
                                checked: false,
                            },
                        ];
                        if (res.data[i].networksupport.includes('00')) {
                            tempNetworkSupportArr[0].checked = true;
                        }
                        if (res.data[i].networksupport.includes('01')) {
                            tempNetworkSupportArr[1].checked = true;
                        }
                        if (res.data[i].networksupport.includes('02')) {
                            tempNetworkSupportArr[3].checked = true;
                        }
                        if (res.data[i].networksupport.includes('03')) {
                            tempNetworkSupportArr[2].checked = true;
                        }
                        if (res.data[i].networksupport.includes('04')) {
                            if (res.data[i].networksupport.includes('05')) {
                                tempNetworkSupportArr[5].name = 'TD/FDD-LTE';
                            } else {
                                tempNetworkSupportArr[5].name = 'TD-LTE';
                            }
                            tempNetworkSupportArr[5].checked = true;
                        } else if (res.data[i].networksupport.includes('05')) {
                            tempNetworkSupportArr[5].checked = true;
                            tempNetworkSupportArr[5].name = 'FDD-LTE';
                        }
                        if (res.data[i].networksupport.includes('06')) {
                            tempNetworkSupportArr[4].checked = true;
                        }
                        if (res.data[i].networksupport.includes('07')) {
                            tempNetworkSupportArr[6].checked = true;
                            tempNetworkSupportArr[6].name = 'WCDMA';
                        }
                        if (res.data[i].networksupport.includes('08')) {
                            tempNetworkSupportArr[6].checked = true;
                            tempNetworkSupportArr[6].name = 'CDMA2000';
                        }
                        if (res.data[i].networksupport.includes('09')) {
                            tempNetworkSupportArr[6].checked = true;
                            tempNetworkSupportArr[6].name = 'TD-SCDMA';
                        }
                        if (res.data[i].networksupport.includes('10')) {
                            tempNetworkSupportArr[7].checked = true;
                            tempNetworkSupportArr[7].name = 'GSM';
                        }
                        if (res.data[i].networksupport.includes('11')) {
                            tempNetworkSupportArr[7].checked = true;
                            tempNetworkSupportArr[7].name = 'CDMA1x';
                        }
                        res.data[i].networksupportValue = tempNetworkSupportArr;
                        res.data[i].apnpoint = JSON.parse(res.data[i].apnpoint);
                        res.data[i].ussd = JSON.parse(res.data[i].ussd);
                        _that.data.operatorArr.push(res.data[i]);
                    }
                    _that.setData({
                        operatorArr: _that.data.operatorArr,
                        apnMaxCount: res.apnMaxCount,
                        countryCount: res.countryCount,
                        operatorNum: res.pager.totalRows,
                        viewNum: res.pager.totalRows,
                    });
                    if (
                        _that.data.operatorArr.length < _that.data.operatorNum
                    ) {
                        _that.setData({
                            loadingList: true,
                        });
                    } else {
                        _that.setData({
                            loadingList: false,
                        });
                    }
                } else {
                    if (res.errmsg == '输入页码超出范围') {
                        _that.setData({
                            loadingAll: true,
                            loading: false,
                        });
                    }
                }
            });
        } else {
            // wx.showToast({
            //     title: '已加载全部',
            //     icon: 'none', //如果要纯文本，不要icon，将值设为'none'
            //     duration: 2000
            // })
        }
    },
    //对比列表
    getOperatorListById() {
        let _that = this;
        let data = {
            authId: app.globalData.authId,
            id: _that.data.id,
        };
        http.getRequest(`cjMap/getCityOperatorListById`, data).then((res) => {
            setTimeout(function () {
                wx.hideLoading();
            }, 1000);
            if (res.status == 0) {
                for (let i = 0; i < res.data.length; i++) {
                    let tempNetworkSupportArr = [
                        {
                            name: 'NR',
                            value: '00',
                            checked: false,
                        },
                        {
                            name: 'ViWiFi',
                            value: '01',
                            checked: false,
                        },
                        {
                            name: 'ViLTE',
                            value: '03',
                            checked: false,
                        },
                        {
                            name: 'VoWiFi',
                            value: '02',
                            checked: false,
                        },
                        {
                            name: 'VoLTE',
                            value: '06',
                            checked: false,
                        },
                        {
                            name: 'LTE',
                            value: '04',
                            checked: false,
                        },
                        {
                            name: '3G',
                            value: '07',
                            checked: false,
                        },
                        {
                            name: '2G',
                            value: '08',
                            checked: false,
                        },
                    ];
                    if (res.data[i].networksupport.includes('00')) {
                        tempNetworkSupportArr[0].checked = true;
                    }
                    if (res.data[i].networksupport.includes('01')) {
                        tempNetworkSupportArr[1].checked = true;
                    }
                    if (res.data[i].networksupport.includes('02')) {
                        tempNetworkSupportArr[3].checked = true;
                    }
                    if (res.data[i].networksupport.includes('03')) {
                        tempNetworkSupportArr[2].checked = true;
                    }
                    if (res.data[i].networksupport.includes('04')) {
                        if (res.data[i].networksupport.includes('05')) {
                            tempNetworkSupportArr[5].name = 'TD/FDD-LTE';
                        } else {
                            tempNetworkSupportArr[5].name = 'TD-LTE';
                        }
                        tempNetworkSupportArr[5].checked = true;
                    } else if (res.data[i].networksupport.includes('05')) {
                        tempNetworkSupportArr[5].checked = true;
                        tempNetworkSupportArr[5].name = 'FDD-LTE';
                    }
                    if (res.data[i].networksupport.includes('06')) {
                        tempNetworkSupportArr[4].checked = true;
                    }
                    if (res.data[i].networksupport.includes('07')) {
                        tempNetworkSupportArr[6].checked = true;
                        tempNetworkSupportArr[6].name = 'WCDMA';
                    }
                    if (res.data[i].networksupport.includes('08')) {
                        tempNetworkSupportArr[6].checked = true;
                        tempNetworkSupportArr[6].name = 'CDMA2000';
                    }
                    if (res.data[i].networksupport.includes('09')) {
                        tempNetworkSupportArr[6].checked = true;
                        tempNetworkSupportArr[6].name = 'TD-SCDMA';
                    }
                    if (res.data[i].networksupport.includes('10')) {
                        tempNetworkSupportArr[7].checked = true;
                        tempNetworkSupportArr[7].name = 'GSM';
                    }
                    if (res.data[i].networksupport.includes('11')) {
                        tempNetworkSupportArr[7].checked = true;
                        tempNetworkSupportArr[7].name = 'CDMA1x';
                    }
                    res.data[i].networksupportValue = tempNetworkSupportArr;
                    res.data[i].apnpoint = JSON.parse(res.data[i].apnpoint);
                    res.data[i].ussd = JSON.parse(res.data[i].ussd);
                    _that.data.operatorArr.push(res.data[i]);
                }
                _that.setData({
                    operatorArr: _that.data.operatorArr,
                    apnMaxCount: res.apnMaxCount,
                    countryCount: res.countryCount,
                    viewNum: _that.data.operatorArr.length,
                });
            }
        });
    },
    // 图片预览
    previewImage(e) {
        if (!e.target.dataset.srcarr.length) return;
        const current = e.target.dataset.src;
        const currentArr = e.target.dataset.srcarr;
        wx.previewImage({
            current,
            urls: currentArr,
        });
    },
    // 复制弹窗显示
    detailShow(e) {
        if (e.currentTarget.dataset.title == '备注说明') {
            this.setData({
                coverTitle: e.currentTarget.dataset.title,
                remarkContent: e.currentTarget.dataset.value,
                phoneDetail: false,
                detailShow: true,
            });
        } else {
            if (e.currentTarget.dataset.value) {
                this.setData({
                    coverTitle: e.currentTarget.dataset.title,
                    coverContent: e.currentTarget.dataset.value,
                    phoneDetail: true,
                    detailShow: true,
                });
            }
        }
    },
    // 复制弹窗隐藏
    detailHide(e) {
        this.setData({
            detailShow: false,
        });
    },
    // 复制
    copyText: function (e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.content,
            success: function (res) {
                wx.getClipboardData({
                    success: function (res) {},
                });
            },
        });
    },
});
